package com.cetool.autocms.controller.sys;

import cn.hutool.core.io.FileUtil;
import com.cetool.autocms.annotation.TokenAnnotation;
import com.cetool.autocms.enumeration.OperateEnum;
import com.cetool.autocms.pojo.dto.sys.MenuListDTO;
import com.cetool.autocms.service.sys.SysService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * @author 戴越
 * @date 2021/11/26
 */
@RestController
@RequestMapping("/sys")
@Api(tags = "系统管理")
public class SysController {
    /**
     * 注入系统管理服务
     */
    @Autowired
    private SysService sysService;

    /**
     * 上传文件
     *
     * @param file 文件
     * @return 操作信息
     * @author 戴越
     * @date 2021/11/29
     */
    @PostMapping("/upload")
    @ApiOperation(value = "上传文件")
    public OperateEnum upload(@RequestParam("file") @NotNull(message = "上传文件不能为空") MultipartFile file) throws IOException {
        String path = "C:\\Users\\Administrator\\Desktop\\app\\nginx-1.18.0\\html\\static\\image\\charts\\";
        //String path = "/Users/daiyue/workspace/upload/";
        String filename = file.getOriginalFilename();
        FileUtil.mkdir(path);
        FileUtil.writeBytes(file.getBytes(), path + "chart_001" + filename.substring(filename.lastIndexOf(".")));
        return OperateEnum.OPERATE_SUCCESS;
    }

    /**
     * 初始化菜单栏
     *
     * @return 菜单栏列表信息传输实体
     * @author 戴越
     * @date 2021/12/2
     */
    @TokenAnnotation
    @PostMapping("/initMenu")
    @ApiOperation(value = "初始化菜单栏")
    public List<MenuListDTO> initMenu() {
        return sysService.initMenu();
    }

}
